<?php
class AdvertisementSearch extends Advertisement {
	/**
	 * Wheather the client of advertisemtn already paid the fee.
	 * @var boolean
	 */
	public $paid;
	
	public $startDateAfter;
	public $startDateBefore;
	public $endDateAfter;
	public $endDateBefore;
	
	const VALUE_NOT_PAID = 0; 
	const VALUE_PAID = 1;
	
	public function rules()
	{
		$parentRules = parent::rules();
		return CMap::mergeArray($parentRules, array(
			array( 'startDateAfter, startDateBefore, endDateAfter, endDateBefore, paid', 'safe', 'on'=>'search'),
		));
	}
	
	public function attributeLabels()
	{
		$parentLabels = parent::attributeLabels();
		return CMap::mergeArray($parentLabels, array(
			'startDateAfter'=>'Start Date After',
			'startDateBefore'=>'Start Date Before',
			'endDateAfter'=>'End Date After',
			'endDateBefore'=>'End Date Before',
			'paid'=>'Paid',
		));
	}
	

	public function search()
	{

		$criteria=new CDbCriteria;

		if(!empty($this->startDateAfter)){
			$criteria->compare('startTime','>='.$this->startDateAfter);
		}
		if(!empty($this->startDateBefore)){
			$criteria->compare('startTime','<='.$this->startDateBefore);
		}
		if(!empty($this->endDateAfter)){
			$criteria->compare('endTime','>='.$this->endDateAfter);
		}
		if(!empty($this->endDateBefore)){
			$criteria->compare('endTime','<='.$this->endDateBefore);
		}
		
		$criteria->compare('number',$this->number,true);
		$criteria->compare('typeId',$this->typeId);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('client',$this->client,true);
		$criteria->compare('endTime',$this->endTime,true);
		$criteria->order='id DESC';//make sure newest listed firstly.
		
		
		if (!empty($this->paid)) {
			if ($this->paid) {
				$criteria->addCondition('paymentDate != null');//in mysql, date cannot contain '' value.
			}else {
				$criteria->addCondition('paymentDate == null');
			}	
		}

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
}